﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Data.SQLite;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;

namespace Clipper.DAL
{
    public class CupDetectResultInformation
    {

        public CupDetectResultInformation()
        {
        }

        /// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int reactionCupID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from CupDetectResultInfomation ");
            strSql.Append(" where id=@id ");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@id", DbType.Int32,4)           };
            parameters[0].Value = reactionCupID;

            int rows = SQLiteHelper.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,cupHoleNum,sampleInfoID,detectItem,detectTime,detectResult,detectList,detectTimeList ");
            strSql.Append(" FROM CupDetectResultInfomation ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            //strSql.Append("order by reactioncupid desc limit 6");

            return SQLiteHelper.Query(strSql.ToString());
        }

        /// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(Model.DataBaseModel.CupDetectResultInformation model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into CupDetectResultInfomation(");
            strSql.Append("cupHoleNum,sampleInfoID,detectItem,detectTime,detectResult,detectList,detectTimeList)");
            strSql.Append(" values (");
            strSql.Append("@cupHoleNum,@sampleInfoID,@detectItem,@detectTime,@detectResult,@detectList,@detectTimeList);");
            //strSql.Append(";select LAST_INSERT_ROWID()");

            SQLiteParameter binParam = new SQLiteParameter("@detectList", DbType.Binary);
            SQLiteParameter binParamtTime = new SQLiteParameter("@detectTimeList", DbType.Binary);
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@cupHoleNum", DbType.Int32),
                    new SQLiteParameter("@sampleInfoID", DbType.Int32),
                    new SQLiteParameter("@detectItem", DbType.String,255),
                    new SQLiteParameter("@detectTime", DbType.DateTime),
                    new SQLiteParameter("@detectResult", DbType.String,20),
                    binParam,
                    binParamtTime
                    };
            //new SQLiteParameter("@description", DbType.String,50),
            //new SQLiteParameter("@checkDatetime", DbType.DateTime),
            //new SQLiteParameter("@lightValue", DbType.Int32),
            //new SQLiteParameter("@note", DbType.String,255)
           
            parameters[0].Value = model.CupHoleNum;
            parameters[1].Value = model.SampleInfoID;
            parameters[2].Value = model.DetectItem;
            parameters[3].Value = model.DetectTime;
            parameters[4].Value = model.DetectResult;

            BinaryFormatter binFormatter = new BinaryFormatter();
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            binFormatter.Serialize(ms, model.DetectResultList);
            binParam.Value = ms.ToArray();

            BinaryFormatter binFormatterTime = new BinaryFormatter();
            System.IO.MemoryStream msTime = new System.IO.MemoryStream();
            binFormatterTime.Serialize(msTime, model.DetectresultTimelist);
            binParamtTime.Value = msTime.ToArray();


            //parameters[5].Value = null;
            //parameters[6].Value = model.description;
            //parameters[7].Value = model.checkDatetime;
            //parameters[8].Value = model.lightValue;
            //parameters[9].Value = model.note;

            int rows = SQLiteHelper.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }

        }

        /// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Clipper.Model.DataBaseModel.CupDetectResultInformation DataRowToModel(DataRow row)
        {
            Clipper.Model.DataBaseModel.CupDetectResultInformation model = new Clipper.Model.DataBaseModel.CupDetectResultInformation();
            if (row != null)
            {
                if (row["id"] != null && row["id"].ToString() != "")
                {
                    model.Id = int.Parse(row["id"].ToString());
                }
                if (row["cupHoleNum"] != null && row["cupHoleNum"].ToString() != "")
                {
                    model.CupHoleNum = int.Parse(row["cupHoleNum"].ToString());
                }
                if (row["sampleInfoID"] != null)
                {
                    model.SampleInfoID = int.Parse(row["sampleInfoID"].ToString());
                }
                if (row["detectItem"] != null && row["detectItem"].ToString() != "")
                {
                    model.DetectItem = row["detectItem"].ToString();
                }
                if (row["detectTime"] != null)
                {
                    model.DetectTime = DateTime.Parse(row["detectTime"].ToString());
                }
                if (row["detectResult"] != null)
                {
                    model.DetectResult = row["detectResult"].ToString();
                }
                if (row["detectList"] != null && row["detectList"].ToString() != "")
                {
                    BinaryFormatter binFormat = new BinaryFormatter();//创建二进制序列化器

                    //model.detectResultList.detectResultList = int.Parse(row["detectList"].ToString());
                    model.DetectResultList = (ObservableCollection<int>)binFormat.Deserialize(new System.IO.MemoryStream((byte[])row["detectList"]));

                }
                if (row["detectTimeList"] != null && row["detectTimeList"].ToString() != "")
                {

                    BinaryFormatter binFormat = new BinaryFormatter();//创建二进制序列化器

                    //model.detectResultList.detectResultList = int.Parse(row["detectList"].ToString());
                    model.DetectresultTimelist = (ObservableCollection<int>)binFormat.Deserialize(new System.IO.MemoryStream((byte[])row["detectTimeList"]));


                }

                //if (row["description"] != null)
                //{
                //    model.description = row["description"].ToString();
                //}
                //if (row["checkDatetime"] != null && row["checkDatetime"].ToString() != "")
                //{
                //    model.checkDatetime = DateTime.Parse(row["checkDatetime"].ToString());
                //}
                //if (row["lightValue"] != null && row["lightValue"].ToString() != "")
                //{
                //    model.lightValue = int.Parse(row["lightValue"].ToString());
                //}
                //if (row["note"] != null)
                //{
                //    model.note = row["note"].ToString();
                //}
            }
            return model;
        }


    }
}
